<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >

      <el-form-item label="工位名称" prop="workstationName">
        <el-input
          v-model="queryParams.workstationName"
          placeholder="请输入工位名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="部门" prop="department">
        <el-tree-select
          v-model="queryParams.department"
          :data="deptTreeList"
          :props="defaultProps"
          check-strictly
          node-key="id"
          placeholder="请选择归属部门"
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item label="订单号" prop="orderCode" v-show="adminUser == '管理员'">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单号"
          clearable
          class="!w-240px"
        />
      </el-form-item>

      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button @click="checkAndCompletePlan" v-show="adminUser == '管理员'"><Icon icon="ep:search" class="mr-5px" />内码数据补全</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column type="index" label="序号" align="center" width="55" />
      <el-table-column label="工位名称" align="center" prop="workstationName" />
      <el-table-column label="部门" align="center" prop="department">
        <template #default="scope">
          {{ deptList.find((dept) => dept.id === scope.row.department)?.name }}
        </template>
      </el-table-column>
      <el-table-column label="工艺顺序" align="center" prop="processOrder" />
      <el-table-column label="工位业务" align="center" prop="business" />
      <el-table-column label="电子秤" align="center" prop="electronicScaleName" />
      <el-table-column label="是否控制" align="center" prop="isControl">
        <template #default="scope">
          {{ scope.row.isControl==1 ? '是' : '否' }}
        </template>
      </el-table-column>
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <WorkstationForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">

import { WorkstationApi, WorkstationVO } from '@/api/hcxly/workstation/Index'
import WorkstationForm from './WorkstationForm.vue'
import * as DeptApi from "@/api/system/dept";
import { defaultProps, handleTree } from '@/utils/tree'
import {useUserStore} from "@/store/modules/user";

/** 工位 列表 */
defineOptions({ name: 'Workstation' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<WorkstationVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  workstationName: undefined,
  department: undefined
})

const userStore = useUserStore()
const adminUser = userStore.getUser.nickname

const queryFormRef = ref() // 搜索的表单

const deptList = ref<DeptApi.DeptVO[]>([])
const deptTreeList = ref<DeptApi.DeptVO[]>([])

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await WorkstationApi.getWorkstationPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await WorkstationApi.deleteWorkstation(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

const checkAndCompletePlan = async () => {
  try {
    await WorkstationApi.checkAndCompletePlan(queryParams.orderCode);
    message.success("操作成功!");
  } catch {

  }
}

/** 初始化 **/
onMounted(async () => {
  getList()
  deptTreeList.value = handleTree(await DeptApi.getSimpleDeptList())
  deptList.value = await DeptApi.getSimpleDeptList();
})
</script>
